<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="skill" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主题 -->
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(scope.data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hDel(scope.data.id)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <depDialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="showDialog"
        :id="curId"
        :isEdit="isEdit"
        @success="hSuccess"
        @close="hClose"
        :originList="originList"
      ></depDialog>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from "@/api/department";
import { array2tree } from "@/utils/index";
import depDialog from "./depDialog.vue";
export default {
  components: { depDialog },
  data() {
    return {
      // 看弹框是否处于编辑状态
      isEdit: false,
      // 树形数据
      list: [],
      showDialog: false, // 是否显示弹层
      // 父部门id
      curId: "",
       originList: []
    };
  },
  created() {
    this.loadDepartments();
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartments();
      console.log(res);
      // 根据业务需要，去掉数组第一个
      res.data.depts.shift();
       this.originList = res.data.depts.map(({id,pid,code,name}) => {
        return {id, name,code, pid}
        })
      //渲染数据
      this.list = array2tree(res.data.depts, "");
    },
    // 点击编辑弹出弹层盒子
    hAdd(id) {
      this.showDialog = true;
      this.curId = id;
      // 传递标识给子组件，告诉它是新增
      this.isEdit = false;
    },
    // 关闭弹窗。更新数据
    hSuccess() {
      this.showDialog = false;
      this.loadDepartments();
    },
    // 调用接口做删除
    async doDel(id) {
      await delDepartments(id);
      this.loadDepartments();
      this.$message.success("删除数据成功");
    },
    // 删除部门
    hDel(id) {
      this.$confirm("此操作将永久删除部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 做删除
          this.doDel(id);
        })
        .catch(() => {});
    },
    // 编辑部门
    hEdit(id) {
      // 弹出弹框
      this.showDialog = true;
      // 表单中的详情数据传给子组件
      this.curId = id;
      // 传递标识给子组件，告诉它是编辑
      this.isEdit = true;
    },
    // 点击取消弹框消失
    hClose() {
      this.showDialog = false;
    },
  },
};
</script>

<style>
</style>